/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 8/26/12
 * Time: 9:47 PM
 * To change this template use File | Settings | File Templates.
 */

function validaCuit(cuit) {
    if (typeof (cuit) == 'undefined')
        return true;
    cuit = cuit.toString().replace(/[-_]/g, "");
    if (cuit == '')
        return true; //No estamos validando si el campo esta vacio, eso queda para el "required"
    if (cuit.length != 11)
        return false;
    else {
        var mult = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
        var total = 0;
        for (var i = 0; i < mult.length; i++) {
            total += parseInt(cuit[i]) * mult[i];
        }
        var mod = total % 11;
        var digito = mod == 0 ? 0 : mod == 1 ? 9 : 11 - mod;
    }
    return digito == parseInt(cuit[10]);
}

function validate() {
    jQuery.validator.addMethod("cuit", validaCuit, 'CUIT/CUIT Inválido');
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, "");
        return this.optional(element) || phone_number.length > 9 &&
            phone_number.match(/^\d{12}$/);
    }, "Please specify a valid phone number");

    $("#formEdit")
        .validate(
        {

            rules : {
                'dni' : {required:true, number:true},
                'name' : "required",
                'surname' : "required",
                'email' : {
                    email : true
                },
                'cuit' : {cuit:true}
            }


        })
}